package com.ocm.tourism.core.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;

@Configurable
@Entity
public class Hotel extends Business {

   private Long convenience;

   @OneToMany(mappedBy="hotel", cascade=CascadeType.ALL, orphanRemoval=true)
   private List<Room> rooms = new ArrayList<Room>();

   @OneToMany(mappedBy="hotel", cascade=CascadeType.ALL, orphanRemoval=true)
   private List<SaleOff> saleOffs = new ArrayList<SaleOff>();

   @OneToMany(mappedBy="hotel", cascade=CascadeType.ALL, orphanRemoval=true)
   private List<Distance> distances = new ArrayList<Distance>();
   
   @OneToOne(cascade=CascadeType.ALL)
   private MapInfo mapInfo;
   
   public MapInfo getMapInfo(){
       return this.mapInfo;
   }
   
   public void setMapInfo(MapInfo mapInfo){
       this.mapInfo = mapInfo;
   }

   public List<Room> getRooms() {
      return rooms;
   }

   public void setRooms(List<Room> rooms) {
      this.rooms = rooms;
   }

   public List<SaleOff> getSaleOffs() {
      return saleOffs;
   }

   public void setSaleOffs(List<SaleOff> saleOffs) {
      this.saleOffs = saleOffs;
   }

   public List<Distance> getDistances() {
      return distances;
   }

   public void setDistances(List<Distance> distances) {
      this.distances = distances;
   }

   public String toString() {
      return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
   }

   public Long getConvenience() {
      return convenience;
   }

   public void setConvenience(Long convenience) {
      this.convenience = convenience;
   }
   
   public void addRoom(Room room){
      room.setHotel(this);
      //detach to fix duplicate row
      rooms.size();
      rooms.add(room);
   }

   public void updateRoom(Room room){
      int roomIndex = this.rooms.indexOf(room);
      room.setHotel(this);
      rooms.set(roomIndex, room);
   }

   public void removeRoom(long roomId){
      Room room = new Room();
      room.setId(roomId);
      rooms.remove(room);
   }

   public void addSaleOff(SaleOff saleOff){
      saleOff.setHotel(this);
      saleOff.setModifiedDate(new Date(System.currentTimeMillis()));
     //detach to fix duplicate row
      saleOffs.size();
      saleOffs.add(saleOff);
   }

   public void updateSaleOff(SaleOff saleOff){
      int index = this.saleOffs.indexOf(saleOff);
      saleOff.setModifiedDate(new Date(System.currentTimeMillis()));
      saleOff.setHotel(this);
      saleOffs.set(index, saleOff);
   }

   public void removeSaleOff(long id){
      SaleOff saleOff = new SaleOff();
      saleOff.setId(id);
      saleOffs.remove(saleOff);
   }
   
   public void addAndUpdateDistances(List<Distance> distances){
      for (Distance distance : distances){
         if (distance.getId() == null || distance.getId() == 0){
            addDistance(distance);
         }else{
            updateDistance(distance);
         }
      }
   }

   public void addDistance(Distance distance){
      distance.setHotel(this);

     //detach to fix duplicate row
      distances.size();
      distances.add(distance);
   }

   public void updateDistance(Distance distance){
      int index = this.distances.indexOf(distance);
      distance.setHotel(this);
      distances.set(index, distance);
   }

   public void addOrUpdateMapInfo(MapInfo mapInfo)
   {
       if (this.mapInfo == null)
          this.mapInfo = new MapInfo();
       
       this.mapInfo.setLatitude(mapInfo.getLatitude());
       this.mapInfo.setLongitude(mapInfo.getLongitude());
   }
}
